package com.pybox.spec;

import com.pybox.entity.TDictData;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
* 描述：TDictData Specification
* @author wuyingkai
* @date 2018-09-15
*/


public class TDictDataSpec implements Specification<TDictData> {

    private Map<String, String> params;

    public TDictDataSpec(Map<String, String> params){
        this.params = params;
    }

    @Override
    public Predicate toPredicate(Root<TDictData> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

        String dictCode = this.params.get("dictCode");
        String dictSort = this.params.get("dictSort");
        String dictLabel = this.params.get("dictLabel");
        String dictValue = this.params.get("dictValue");
        String dictType = this.params.get("dictType");
        String cssClass = this.params.get("cssClass");
        String listClass = this.params.get("listClass");
        String isDefault = this.params.get("isDefault");
        String status = this.params.get("status");
        String createBy = this.params.get("createBy");
        String createTime = this.params.get("createTime");
        String updateBy = this.params.get("updateBy");
        String updateTime = this.params.get("updateTime");
        String remark = this.params.get("remark");
        List<Predicate> ps = new ArrayList<>();

        if (!StringUtils.isEmpty(dictCode)) {
            ps.add(criteriaBuilder.like(root.get("dictCode"), "%" + dictCode + "%"));
        }
        if (!StringUtils.isEmpty(dictSort)) {
            ps.add(criteriaBuilder.like(root.get("dictSort"), "%" + dictSort + "%"));
        }
        if (!StringUtils.isEmpty(dictLabel)) {
            ps.add(criteriaBuilder.like(root.get("dictLabel"), "%" + dictLabel + "%"));
        }
        if (!StringUtils.isEmpty(dictValue)) {
            ps.add(criteriaBuilder.like(root.get("dictValue"), "%" + dictValue + "%"));
        }
        if (!StringUtils.isEmpty(dictType)) {
            ps.add(criteriaBuilder.like(root.get("dictType"), "%" + dictType + "%"));
        }
        if (!StringUtils.isEmpty(cssClass)) {
            ps.add(criteriaBuilder.like(root.get("cssClass"), "%" + cssClass + "%"));
        }
        if (!StringUtils.isEmpty(listClass)) {
            ps.add(criteriaBuilder.like(root.get("listClass"), "%" + listClass + "%"));
        }
        if (!StringUtils.isEmpty(isDefault)) {
            ps.add(criteriaBuilder.like(root.get("isDefault"), "%" + isDefault + "%"));
        }
        if (!StringUtils.isEmpty(status)) {
            ps.add(criteriaBuilder.like(root.get("status"), "%" + status + "%"));
        }
        if (!StringUtils.isEmpty(createBy)) {
            ps.add(criteriaBuilder.like(root.get("createBy"), "%" + createBy + "%"));
        }
        if (!StringUtils.isEmpty(createTime)) {
            ps.add(criteriaBuilder.like(root.get("createTime"), "%" + createTime + "%"));
        }
        if (!StringUtils.isEmpty(updateBy)) {
            ps.add(criteriaBuilder.like(root.get("updateBy"), "%" + updateBy + "%"));
        }
        if (!StringUtils.isEmpty(updateTime)) {
            ps.add(criteriaBuilder.like(root.get("updateTime"), "%" + updateTime + "%"));
        }
        if (!StringUtils.isEmpty(remark)) {
            ps.add(criteriaBuilder.like(root.get("remark"), "%" + remark + "%"));
        }

        criteriaQuery = criteriaQuery.where(ps.toArray(new Predicate[ps.size()]));

        return criteriaQuery.getRestriction();
    }

}